package com.shundr.shipper.common;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.EditText;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.shundr.shipper.R;
import com.shundr.shipper.base.BaseActivity;
import com.shundr.shipper.common.http.CommonRequest;
import com.shundr.shipper.common.util.ProgressDialogHelper;
import com.shundr.shipper.common.util.ToastHelper;
import com.shundr.shipper.frame.util.StringUtil;

/**
 * 意见反馈
 * FeedbackActivity
 * 
 * @author larry 2015-6-29 下午2:55:25 
 * @version 1.0.0
 *
 */
public class FeedbackActivity extends BaseActivity {

	private EditText etContent;
	private EditText etPhone;
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case CommonRequest.GIVE_ADVICE_SUCCEED:
                try {
					ToastHelper.toastShort(mContext, "提交成功");
					finish();
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			case CommonRequest.REQUEST_FAILED:
				try {
					ToastHelper.toastShort(mContext,(String)msg.obj);
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			default:
				break;
			}
			ProgressDialogHelper.dismiss();
			super.handleMessage(msg);
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_feedback);
		etContent = (EditText) findViewById(R.id.et_feedback_content);
		etPhone = (EditText) findViewById(R.id.et_feedback_contact);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.menu_confirm, menu);
		menu.getItem(0).setTitle("提交");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_confirm:
			giveAdvice();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void giveAdvice() {
		try {
			String content = etContent.getText().toString().trim();
			if (StringUtil.isNull(content)) {
				ToastHelper.toastShort(mContext, "请输入你的意见");
				return;
			}
			String contact = etPhone.getText().toString().trim();
			ProgressDialogHelper.show(mContext, "正在提交中...");
			new CommonRequest(mContext, mHandler).giveAdvice(content, contact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	protected void onDestroy() {
		try {
			mHandler.removeCallbacksAndMessages(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.onDestroy();
	}
}
